import { ElMessage, ElMessageBox, type Action } from "element-plus"
import { userStore } from "../../store/userStore";
import { friendStore } from "../../store/friendStore";
import type { FriendRequest, UserFriendInfo } from "../../types/types";
import { groupMemberIds, infoValue, pullGroupInfo } from "../../components/General/Contact/contact";
export const sendQuitGroupMessage = (websocket: WebSocket, messageInfo: { messageType: string; groupId: string; memberId: string; groupName: string; }) => {
    invalidWebSocket(websocket);
    // 使用一个消息弹出框
    try {
        websocket.send(JSON.stringify(messageInfo));
    } catch (error) {
        console.error(error);
    }
}
import { v4 as uuidv4 } from 'uuid'
import type { Client } from "@stomp/stompjs";
import { sendStompMessage } from "../stomp/websocketStomp";


// 创建群聊的消息

export const sendCreateGroup = (websocket: WebSocket, groupType: string): void => {
    // 获取当前用户信息
    const UserInfo = userStore().userInfo;
    const leaderId = UserInfo.uu_id;

    // 确保当前用户信息存在
    if (!UserInfo?.uu_id || !UserInfo?.username || !UserInfo?.avatar) {
        ElMessage.error("无法获取当前用户信息");
        return;
    }

    // 清空群成员列表，避免重复添加         这个一定要加，不然群成员信息会叠加
    pullGroupInfo.group_members = [];

    // 添加群主信息到成员列表
    pullGroupInfo.group_leader = UserInfo.uu_id!;
    pullGroupInfo.group_members!.push({
        key: UserInfo.uu_id!,
        label: UserInfo.username!,
        avatar: UserInfo.avatar!
    });

    // 添加其他群成员的详细信息
    groupMemberIds.value.forEach((member_id: string) => {
        // 获取群成员基本信息
        const memberBasicInfo = friendStore().getLabelAndAvatarByUUID(member_id);
        if (memberBasicInfo) {
            // 添加到群成员列表中
            pullGroupInfo.group_members!.push(memberBasicInfo);
        }
    });

    if (groupType === 'pull') {
        // 检查群成员数量
        if (pullGroupInfo.group_members!.length < 2) {
            ElMessage.error("群成员至少需要两位！");
            return;
        }

        // 对群信息进行基本判断
        if (pullGroupInfo.group_name.trim() === "" || pullGroupInfo.group_type.trim() === "") {
            ElMessage.warning("群聊信息不完整！");
            return;
        }

        // 创建群聊
        // await createGroup(leaderId!, pullGroupInfo);
        // 其实修改之后就是在这里修改而已，替换为向websocket发送信息
        const stompMessage = {
            messageType: "snow-create-group",
            groupInfo: pullGroupInfo
        }
        try {
            websocket.send(JSON.stringify(stompMessage));
        } catch (error) {
            console.error(error);
        }
        // sendStompMessage("create-group", stompMessage);
    } else {
        // 清空群聊信息，等同于重置信息
        pullGroupInfo.group_name = "";
        pullGroupInfo.group_type = "";
        pullGroupInfo.group_members = [];
        pullGroupInfo.group_leader = "";
        pullGroupInfo.group_id = uuidv4();
        infoValue.value = [];
    }
}

// 发送邀请用户进群的信息
export const inviteJoinGroupChat = (websocket: WebSocket, memberList: string[], groupId: string, groupName: string) => {
    // 1、验证websocket是否有效
    invalidWebSocket(websocket);
    // 2、通过memberList获取好友的label、avatar信息，这是最简单的拉取群聊方式
    const friendStoreInstance = friendStore();
    const friendsInfoList = memberList.map((memberId: string) => {
        const friend = friendStoreInstance.storageUserFriendList.find((friend: UserFriendInfo) => {
            return friend.friendDetails.uu_id === memberId;
        })
        if (friend) {
            return {
                key: friend.friendDetails.uu_id,
                label: friend.friendDetails.username,
                avatar: friend.friendDetails.avatar
            };
        }
    }).filter((friendInfo): friendInfo is { key: string; label: string; avatar: string; } => {
        return friendInfo !== undefined || friendInfo !== null;
    })
    // 2、构建消息对象
    const message = {
        messageType: "invite-to-join-the-group-chat",
        groupId: groupId,
        groupName: groupName,
        memberList: friendsInfoList,
        userNickName: userStore().userInfo.username
    }
    try {
        websocket.send(JSON.stringify(message));
    } catch (error) {
        console.error(error);
        // }
    }
}


// 同意好友请求的信息
export const acceptFriendRequest = (websocket: WebSocket, requestItem: FriendRequest) => {
    invalidWebSocket(websocket);
    const message = {
        messageType: "accept-friend-request",
        fromUserId: requestItem.fromUserId,
        requestId: requestItem.requestId
    }
    try {
        websocket.send(JSON.stringify(message));
    } catch (error) {
        console.error(error);
    }
}




// 验证websocket是否异常的函数
function invalidWebSocket(websocket: WebSocket) {
    if (!websocket || websocket.readyState !== WebSocket.OPEN) {
        ElMessage.warning("websocket异常!");
        return;
    }
}



// 验证StompClient连接状态


